package com.baizhi.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * ClassName: TestController
 * Description:
 * Date: 2022/11/03 PM 11:19
 * <p>
 * project: codes
 * package: com.baizhi.controller
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@RestController
public class TestController {

    @RequestMapping("/user")
    public String user() {
        // 1. 从代码中获取用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        // 2.通过获取用户信息
        User user = (User) authentication.getPrincipal();
        System.out.println("username= " + user.getUsername());
        System.out.println("authorities= " + user.getAuthorities());
        return "user ok";
    }
}
